﻿namespace ExamV5.ManagSys
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public class ManagWorkerList : Page
    {
        protected Button butAdd;
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLType;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtName;
        protected UpdatePanel UPPart;

        protected AspNetPager aspPager;
        protected void butAdd_Click(object sender, EventArgs e)
        {
            if (this.ViewState["UserPower"].ToString() == "Y")
            {
                if (!(this.txtName.Text.Trim() != "") || !(this.DDLType.SelectedItem.Value != ""))
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('请选择所属类别或输入名称信息！')", true);
                }
                else if (this.publicFun_0.GetValues("select ID from SysParmInfo where ParmName='" + this.publicFun_0.CheckString(this.txtName.Text) + "' and ParmMemo='" + this.DDLType.SelectedItem.Value + "'", "ID") != "")
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BHave", "alert('此" + this.DDLType.SelectedItem.Text + "名称已经存在！')", true);
                }
                else
                {
                    this.publicFun_0.ExecuteSql("insert into SysParmInfo(ParmName,ParmValue,ParmMemo) values('" + this.publicFun_0.CheckString(this.txtName.Text) + "','" + this.publicFun_0.CheckString(this.txtName.Text) + "','" + this.DDLType.SelectedItem.Value + "')");
                    this.method_0();
                    this.txtName.Text = "";
                    this.publicFun_0.ExecuteSql("insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('添加职务岗位信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')");
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BAdd", "alert('增加成功！')", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('用户管理权限未开放！')", true);
            }
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.aspPager.CurrentPageIndex = 1;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.DataGridInfo.EditIndex = -1;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.DataItemIndex == -1)
            //{
            //    this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
            //    this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            //}
            //else
            //{
            //    e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
            //    if ((e.Row.DataItemIndex % 2) != 0)
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
            //    }
            //    else
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
            //    }
            //}
            if ((e.Row.RowIndex != -1) && !((e.Row.RowState != DataControlRowState.Edit) ? (e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate)) : false))
            {
                DropDownList list = (DropDownList) e.Row.Cells[0].FindControl("DDLGridType");
                list.SelectedIndex = -1;
                list.Items.FindByText(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ParmMemo"].ToString()).Selected = true;
                TextBox box = (TextBox) e.Row.Cells[1].Controls[0];
                box.Width = 250;
                box.CssClass = "text";
                box.Height = 0x17;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton button = (LinkButton) e.Row.Cells[3].FindControl("LBDel");
                if (this.DataGridInfo.DataKeys[e.Row.RowIndex]["ParmMemo"].ToString().Trim() == "职务名称")
                {
                    button.Attributes.Add("onclick", "javascript:{if(confirm('确定删除职务信息[" + e.Row.Cells[1].Text + "]吗？')==false) return false;}");
                }
                else
                {
                    button.Attributes.Add("onclick", "javascript:{if(confirm('确定删除岗位信息[" + e.Row.Cells[1].Text + "]吗？')==false) return false;}");
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (this.ViewState["SortType"].ToString() != " Desc")
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ParmName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "ParmMemo":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ParmName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "ParmMemo":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                }
            }
        }

        protected void DataGridInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string str = this.DataGridInfo.DataKeys[e.RowIndex]["ID"].ToString();
            string str2 = this.DataGridInfo.DataKeys[e.RowIndex]["ParmName"].ToString();
            if (!(this.DataGridInfo.DataKeys[e.RowIndex]["ParmMemo"].ToString().Trim() == "职务名称"))
            {
                if (this.publicFun_0.GetValues("select ID from UserInfo where UserGWName='" + str2 + "'", "ID") != "")
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BHave", "alert('" + str2 + "岗位存在人员信息,无法删除！')", true);
                    return;
                }
            }
            else if (this.publicFun_0.GetValues("select ID from UserInfo where Worker='" + str2 + "'", "ID") != "")
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BHave", "alert('" + str2 + "职务存在人员信息,无法删除！')", true);
                return;
            }
            this.publicFun_0.ExecuteSql("delete SysParmInfo where ID=" + str);
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.DataGridInfo.EditIndex = e.NewEditIndex;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string str = this.DataGridInfo.DataKeys[e.RowIndex]["ID"].ToString();
                string strSql = "";
                DropDownList list = (DropDownList) this.DataGridInfo.Rows[e.RowIndex].Cells[0].FindControl("DDLGridType");
                TextBox box = (TextBox) this.DataGridInfo.Rows[e.RowIndex].Cells[1].Controls[0];
                box.Width = 150;
                box.CssClass = "text";
                box.Height = 0x13;
                if ((list.SelectedIndex != -1) ? !(box.Text.Trim() == "") : false)
                {
                    if (list.SelectedItem.Value.Trim() == "Worker")
                    {
                        strSql = "update SysParmInfo set ParmName='" + this.publicFun_0.CheckString(box.Text) + "',ParmValue='" + this.publicFun_0.CheckString(box.Text) + "',ParmMemo='" + list.SelectedItem.Value + "' where ID=" + str + ";update UserInfo set Worker='" + box.Text.Trim() + "' where Worker='" + this.DataGridInfo.DataKeys[e.RowIndex]["ParmName"].ToString() + "'";
                    }
                    else
                    {
                        strSql = "update SysParmInfo set ParmName='" + this.publicFun_0.CheckString(box.Text) + "',ParmValue='" + this.publicFun_0.CheckString(box.Text) + "',ParmMemo='" + list.SelectedItem.Value + "' where ID=" + str + ";update UserInfo set UserGWName='" + box.Text.Trim() + "' where UserGWName='" + this.DataGridInfo.DataKeys[e.RowIndex]["ParmName"].ToString() + "'";
                    }
                    this.publicFun_0.ExecuteSql(strSql);
                    this.DataGridInfo.EditIndex = -1;
                    this.method_0();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('所属类别和名称信息不可以为空！')", true);
                }
            }
            catch
            {
            }
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (this.ViewState["SortFiled"].ToString() == sortExpression)
            {
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    this.ViewState["SortType"] = " Desc";
                }
                else
                {
                    this.ViewState["SortType"] = " ASC";
                }
            }
            else
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_PreRender(object sender, EventArgs e)
        {
            // 使用<TH>替换<TD> 
            this.DataGridInfo.UseAccessibleHeader = true;
            //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            this.DataGridInfo.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected void aspPager_PageChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = this.aspPager.CurrentPageIndex - 1;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DDLType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        private void method_0()
        {
            string selectCommandText = "";
            if (!(this.ViewState["UserPower"].ToString() == "Y"))
            {
                selectCommandText = "select  ID,ParmName,ParmValue,ParmMemo=case ParmMemo when 'Worker' then '职务名称' when 'GWName' then '岗位名称' end  from SysParmInfo where  ID=0 ";
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('用户管理权限未开放！')", true);
            }
            else
            {
                selectCommandText = "select ID,ParmName,ParmValue,ParmMemo=case ParmMemo when 'Worker' then '职务名称' when 'GWName' then '岗位名称' end  from SysParmInfo where ID<>0 ";
            }
            if (this.DDLType.SelectedItem.Value != "")
            {
                selectCommandText = selectCommandText + " and ParmMemo='" + this.DDLType.SelectedItem.Value + "'";
            }
            else
            {
                selectCommandText = selectCommandText + " and (ParmMemo='Worker' or ParmMemo='GWName')";
            }
            if (this.txtName.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and ParmName like '%" + this.txtName.Text.Trim() + "%'";
            }
            selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            //this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            this.labRescordCount.Text = "总共&nbsp;<i>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</i>&nbsp;条记录&nbsp;<b>" + Convert.ToString((int)(this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b>&nbsp;页&nbsp;";
            this.aspPager.RecordCount = dataSet.Tables["DataInfo"].Rows.Count;
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) && !(this.httpCookie_0.Value == ""))
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                    else
                    {
                        this.txtName.Attributes.Add("onkeydown", "SubmitKeyClick('butAdd');");
                        this.ViewState["UserPower"] = this.publicFun_0.GetValues("select ManagUser from UserPower where UserCode='" + this.httpCookie_0.Value + "'", "ManagUser");
                        this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                        this.ViewState["SortFiled"] = "ID";
                        this.ViewState["SortType"] = " Desc";
                        this.method_0();
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

